package demo7;

class MyThread extends Thread{
    @Override
    public void run() {
        long r = 0;
        for (long j = 0; j < 100_0000_0000L; j++) {
            r += j;
        }
        System.out.println("计算结束");
    }
}
public class MultiThreadDemo {
    public static void main(String[] args) throws InterruptedException {
        // 计算 0 + 1 + 2 + 3 + ... + 10000000W)
        // 这个计算持续 10 次
        // 计算其耗时情况

        long b = System.currentTimeMillis();

        Thread[] threads = new Thread[9];
        for (int i = 0; i < 9; i++){
            threads[i] = new MyThread();
            threads[i].start();
        }
        long r = 0;
        for (long j = 0; j < 100_0000_0000L; j++) {
            r += j;
        }
        System.out.println("计算结束");
        for (int i = 0; i < 9; i++){
            threads[i].join();
        }

        long e = System.currentTimeMillis();
        double t = (e - b) / 1000.0;
        System.out.printf("耗时：%.4f s", t);
    }
}
